package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.SysMessage;
import cn.cdeden.knowledge.domain.dto.SysMessageDto;
import cn.cdeden.knowledge.domain.dto.SysMessageQueryParam;
import cn.cdeden.knowledge.domain.vo.SysMessageVo;
import cn.cdeden.knowledge.mapper.SysMessageMapper;
import cn.cdeden.knowledge.service.SysMessageService;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.resource.api.RemoteMessageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

//    private final RedisUtils redisUtils;
    private final SysMessageMapper sysMessageMapper;

    @DubboReference
    private final RemoteMessageService remoteMessageService;
    @Override
    public TableDataInfo<SysMessageVo> getMessageByUser(SysMessageQueryParam query, Pageable pageable) {
        IPage<SysMessage> queryPage = PageUtil.toMybatisPage(pageable);
        query.setRecipient(String.valueOf(LoginHelper.getUserId()));
        IPage<SysMessage> iPage = sysMessageMapper.getMessageByUser(query,queryPage);
        TableDataInfo<SysMessage> build = TableDataInfo.build(iPage);
        TableDataInfo<SysMessageVo> sysMessageVoTableDataInfo = MapstructUtils.convertTableDataInfo(build, SysMessageVo.class);
        return sysMessageVoTableDataInfo;
    }

    @Override
    public void addMessage(SysMessageDto sysMessageDto) {
        SysMessage convert = MapstructUtils.convert(sysMessageDto, SysMessage.class);
        sysMessageMapper.insert(convert);
        remoteMessageService.publishMessage(List.of(Long.valueOf(sysMessageDto.getRecipient())),sysMessageDto.getContent());
    }
    @Override
    public boolean readMessage(Long id) {
        int update = sysMessageMapper.update(new LambdaUpdateWrapper<SysMessage>()
            .eq(SysMessage::getId, id)
            .set(SysMessage::getReadingTime, new Date())
            .set(SysMessage::getReading, true));
        return  true;
    }
    @Override
    public boolean readMessageAll(String userId) {
        int update = sysMessageMapper.update(new LambdaUpdateWrapper<SysMessage>()
            .eq(SysMessage::getRecipient, userId)
            .set(SysMessage::getReadingTime, new Date())
            .set(SysMessage::getReading, true));
        return true;
    }

}
